package com.lswms.modules.lswms.service.impl.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lswms.common.page.PageData;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.modules.lswms.dao.base.PutawaystrategydetailDao;
import com.lswms.modules.lswms.dto.base.PutawaystrategydetailDTO;
import com.lswms.modules.lswms.entity.base.PutawaystrategydetailEntity;
import com.lswms.modules.lswms.entity.orders.OrderdetaillistEntity;
import com.lswms.modules.lswms.service.base.PutawaystrategydetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 上架策略明细
 *
 * @author DX sunlightcs@gmail.com
 * @since 1.0.0 2021-06-21
 */
@Service
public class PutawaystrategydetailServiceImpl extends CrudServiceImpl<PutawaystrategydetailDao, PutawaystrategydetailEntity, PutawaystrategydetailDTO> implements PutawaystrategydetailService {

    @Override
    public QueryWrapper<PutawaystrategydetailEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String strategykey = (String)params.get("strategykey");
        
        QueryWrapper<PutawaystrategydetailEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.eq(StringUtils.isNotBlank(strategykey), "strategykey", strategykey);
        
        wrapper.orderByDesc("strategykey") ;
        wrapper.orderByAsc("orderline") ;
        
        return wrapper;
    }


    @Override
	public PageData<PutawaystrategydetailEntity> pageBySql(Map<String, Object> params) {
		IPage<PutawaystrategydetailEntity> page = baseDao.selectPageBySql(getPage(params, null, false), getWrapper(params));
		return getPageData(page, PutawaystrategydetailEntity.class);
	}
    
    
    @Override
    public Integer getPutawaystrategydetailLine(String strategykey) {
            Integer count  =0;
         if(StringUtils.isNotBlank(strategykey)) {
            // QueryWrapper<ReceiptdetailEntity> wrapper = new QueryWrapper<>();
               //  wrapper.eq(StringUtils.isNotBlank(receiptkey), "receiptkey", receiptkey);
               //  count = baseDao.selectCount(wrapper) ;
                 count = baseDao.getPutawaystrategydetailMaxLine(strategykey) ;
         }

            return count;
    }


	@Override
	public String valideAndSave(PutawaystrategydetailDTO dto) {

		String sort = dto.getSkusort();
		if (StringUtils.isNotBlank(sort)) {
			String[] sortArray = sort.split("#");
			sort = sortArray[sortArray.length - 1];
			dto.setSkusort(sort);
		}
		
		Integer line = getPutawaystrategydetailLine(dto.getStrategykey()) ;
		dto.setLinenum(line+1);

		save(dto);
		
		
		return null;
	}


	@Override
	public String valideAndUpdate(PutawaystrategydetailDTO dto) {
		String sort = dto.getSkusort();
		if (StringUtils.isNotBlank(sort)) {
			String[] sortArray = sort.split("#");
			sort = sortArray[sortArray.length - 1];
			dto.setSkusort(sort);
		}
		update(dto);
		
		return null;
	}

}